package com.easygem.front.service.imp;

import com.easygem.common.error.SystemCodeEnum;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.front.ClassInfo;
import com.easygem.common.vo.front.ClassVO;
import com.easygem.common.vo.system.PageVO;
import com.easygem.front.mapper.ClassInfoMapper;
import com.easygem.front.service.IClassInfoService;
import com.easygem.front.service.base.AbstractServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;


@Service
public class ClassInfoServiceImpl extends AbstractServiceImpl<ClassInfoMapper, ClassInfo, Integer> implements IClassInfoService {
    @Autowired
    private ClassInfoMapper classInfoMapper;

    @Override
    public PageVO<ClassInfo> findList(Integer pageNum, Integer pageSize, ClassVO classVo) {
        //已经拥有的
        PageHelper.startPage(pageNum,pageSize);
//        Example o = new Example(ClassInfo.class);
//        String className = classVo.getClassname();
//        Integer clsStatus = classVo.getClsstatus();
//        Example.Criteria criteria = o.createCriteria();
//        if(!StringUtils.isEmpty(className)){
//            criteria.andLike("classname","%"+className+"%");
//        }
//        if(clsStatus!=null){
//            criteria.andEqualTo("clsstatus",clsStatus);
//        }
//
//        List<ClassInfo> classList= classInfoMapper.selectByExample(o);
//        classList.stream().forEach(classInfo -> {
//            classInfo.setMaster(classInfoMapper.getEnpName(classInfo.getHeadmaster()));
//            classInfo.setInstname(classInfoMapper.getEnpName(classInfo.getInstructor()));
//            classInfo.setGuidename(classInfoMapper.getEnpName(classInfo.getEmploymentguide()));
//        });
//        PageInfo<ClassInfo> info=new PageInfo<>(classList);
//        return new PageVO<>(info.getTotal(),classList);

        //用连表查询的方式
        ClassInfo classInfo = new ClassInfo();
        BeanUtils.copyProperties(classVo,classInfo);
        classInfo.setClassname(classVo.getClassname());
        classInfo.setClsstatus(classVo.getClsstatus());
        List<ClassInfo> list = classInfoMapper.selectClassList(classInfo);
        PageInfo<ClassInfo> info=new PageInfo<>(list);
        return new PageVO<>(info.getTotal(),list);

    }

    @Override
    public String getEmpNameById(Integer id) {
        return classInfoMapper.getEnpName(id);
    }

    @Override
    public boolean add(ClassInfo classInfo) {
        return classInfoMapper.insert(classInfo)>0;
    }

    @Override
    public ClassInfo getClassById(Integer classid) {
        return classInfoMapper.selectByPrimaryKey(classid);
    }

    @Override
    public void deleteById(Integer classid) {
        classInfoMapper.deleteByPrimaryKey(classid);
    }

    @Override
    public void update(Integer classid, ClassVO classVO) throws SystemException {
        ClassInfo classInfo = classInfoMapper.selectByPrimaryKey(classid);
        @NotNull(message = "班级不能为空") Integer classId = classInfo.getClassid();
        if(classInfo==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的班级不存在");
        }
        BeanUtils.copyProperties(classVO,classInfo);
        classInfo.setClassid(classid);
        classInfoMapper.updateByPrimaryKeySelective(classInfo);
    }

    @Override
    public List<ClassVO> findAll() {
        List<ClassInfo> classInfos = classInfoMapper.selectAll();
        //转vo
        List<ClassVO> classVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(classInfos)) {
            for (ClassInfo classInfo : classInfos) {
                ClassVO c = new ClassVO();
                BeanUtils.copyProperties(classInfo, c);
                c.setClassId(classInfo.getClassid());
                c.setClassname(classInfo.getClassname());
                Example o = new Example(ClassInfo.class);
                o.createCriteria().andEqualTo("classid",classInfo.getClassid());
                classVOS.add(c);
            }
        }
        return classVOS;
    }
}
